﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nodeApp
{
     public class World
    {
        private Dictionary<String, Node> nodesStored;
        private List<Edge> edgeCollection;
        private mappedArray weightGraph;
        public int numberOfNodes {get;set;}
        private int heightWorld;
        private int widthWorld;
        
        public World(int worldHeight, int worldWidth, int nodeSize)
        {
            heightWorld = worldHeight;
            widthWorld = worldWidth;
            this.nodesStored = CollectionFactory.buildNodeDictionary(worldWidth, worldHeight, nodeSize);
            this.numberOfNodes = nodesStored.Count;
            this.weightGraph = new mappedArray(this.numberOfNodes);
            this.edgeCollection = new List<Edge>();
        }

        public Dictionary<String, Node> getNodeCollection()
        {
            return this.nodesStored;
        }

        /// <summary>
        /// adds a new edge to the given map representing the world
        /// </summary>
        /// <param name="startNodeCoord">coordinate of the starting node</param>
        /// <param name="endNodeCoord">coordinate of the end node</param>
        /// <param name="weight">weight of the new edge</param>
        public void addNewEdge(string startNodeCoord, string endNodeCoord, int weight)
        {
            this.edgeCollection.Add(new Edge(weight, this.nodesStored[startNodeCoord],this.nodesStored[endNodeCoord]));
        }

        public void MST()
        {
            this.edgeCollection = this.MST(this.edgeCollection, this.nodesStored.Values.ToList());
        }


        public List<Edge> MST(List<Edge> edges,List<Node> nodes)
        {
            List<Edge> tempList = new List<Edge>();
            foreach (Node N in nodes)
            {
                Edge tempEdge = null;
                foreach(Edge E in edges)
                {
                    if (E.Contains(N))
                    {
                        if (tempEdge == null)
                        {
                            tempEdge = E;
                        }
                        else
                        {
                            if (tempEdge.weight > E.weight && !tempList.Contains(E))
                            {
                                tempEdge = E;
                            }
                        }
                    }

                }

                while(!tempList.Contains(tempEdge))
                {
                    tempList.Add(tempEdge);
                }
            }
            return tempList;
        }
        public void test()
        {
            List<Node> nlist = new List<Node>();
            Node a = new Node(5, 5, 0);
            Node b = new Node(10, 10, 1);
            Node c = new Node(15, 5, 2);
            Node d = new Node(0, 0, 3);
            Node e = new Node(0, 5, 4);
            Node f = new Node(10, 0, 5);
            Node g = new Node(15, 0, 6);
            Node h = new Node(20, 0, 7);
            nlist.Add(a);
            nlist.Add(b);
            nlist.Add(c);
            nlist.Add(d);
            nlist.Add(e);
            nlist.Add(f);
            nlist.Add(g);
            nlist.Add(h);

            List<Edge> elist = new List<Edge>();
            elist.Add(new Edge(2, a, b));
            elist.Add(new Edge(5,b,c));
            elist.Add(new Edge(10, c, d));
            elist.Add(new Edge(12,d,e));
            elist.Add(new Edge(11, e, f));
            elist.Add(new Edge(9, f, c));
            elist.Add(new Edge(6, g, f));
            elist.Add(new Edge(7, h, f));
            elist.Add(new Edge(8, h, g));
            elist.Add(new Edge(3, a, h));
            elist.Add(new Edge(8, b, g));
            

            elist = this.MST(elist,nlist);

            foreach (Edge edg in elist)
            {
                Console.WriteLine(edg.weight.ToString()+", "+edg.startNode.nodeNumber.ToString()+", "+edg.endNode.nodeNumber.ToString());
            }
        }
    }
}
